var aid;
var title;
var message;
var publishDate;
var type;
var imgFile;
var videoLink;




function modifyAnnouncementForm(){
    var array = [];
    var allValidated = true;
    if (nonEmptyValidation(title)) {
        array.push(1);
    } else {
        array.push(0);
    }
    if (nonEmptyValidation(message)) {
        array.push(1);
    } else {
        array.push(0);
    }
    if (validateFileType(imgFile)) {
        array.push(1);
    } else {
        array.push(0);
    }
    if (validateYoutubeURL(videoLink)) {
        array.push(1);
    } else {
        array.push(0);
    }
    
    for (var i = 0; i < array.length; i++) {
        console.log('index ' + i + ' : ' + array[i]);
        if (array[i] === 0) {
            console.log('0 found at index ' + i);
            allValidated = false;
//            break;
        }
    }
    
    if(allValidated){
        var imgVal = $('#imgFile').val();
        if(imgVal==='')
        {
            alert("empty input file");

        }else{
            var fullImgPath = $('#imgFile').val();
            alert(fullImgPath);
            var filename = /([^\\]+)$/.exec(fullImgPath)[1];
            alert(filename);
            var elem = document.getElementById("imgName");
            elem.value = filename; 
        }
        
        $.ajax({
            type: 'POST',
            url: 'action.php',
            data: $('#announcementupdateform').serialize(),
            success: function(result){
                if(!result.error){
                    $('#updateErr').fadeIn('fast').addClass('success').removeClass('alert').html(result.message).delay(4000).fadeOut('slow');
                }else{
                     $('#updateErr').fadeIn('fast').addClass('alert').removeClass('success').html(result.message).delay(4000).fadeOut('slow');
                }
            }
        });
    }else{
        alert('nonValidated');
    }
}

function imageName(){
    var fullImgPath = $('#imgFile').val();
    var filename = /([^\\]+)$/.exec(fullImgPath)[1];
    return filename;
    document.getElementById('imgFile').value = filename;
}

$(document).ready(function() {
    $("#dob").datepicker({
        minDate: new Date(1900, 1 - 1, 1),
        maxDate: "+0",
        changeMonth: true,
        changeYear: true,
        dateFormat: "yy-mm-dd",
        yearRange: "1900:+0",
        hideIfNoPrevNext: true,
        showButtonPanel: true,
        showOptions: {direction: "down"}
    });
    username = document.getElementById('user');
    title = document.getElementById('title');
    message = document.getElementById('message');
    imgFile = document.getElementById('imgFile');
    videoLink = document.getElementById('videoLink');
    
});


function checkBoxTicked(){
    var chk = document.getElementById('a1');
    alert(chk);
    if(chk.checked){
        alert('checked');
    }else{
        alert('unchecked');
    }
    alert('out');
}


function nonEmptyValidation(element) {
    var elementLength = element.value.length;
    var info = document.getElementById(element.id + "Err");
    if (elementLength === 0) {
        $('#' + element.id).addClass('error');
        $("#" + info.id).text("Required Field").show();
        return false;
    } else {
        $('#' + element.id).removeClass('error');
        $('#' + info.id).hide();
        return true;
    }
}

function validateFileType(element) {
    var filename=document.getElementById(element.id).value;
    var extension=filename.substr(filename.lastIndexOf('.')+1).toLowerCase();
    if(extension===""){
        return true;
    }else if(extension==='jpg' || extension==='gif' || extension==='png' || extension==='bmp' || extension==='tiff') {
        $('#' + element.id + 'Err').addClass('alert').removeClass('success').fadeOut('fast');
        //alert($("#"+element.id).val());
        return true;
    } else {
        $('#' + element.id + 'Err').fadeIn('fast').addClass('alert').removeClass('success');
        //alert($("#"+element.id).val());
        return false;
    }
}
            
function validateYoutubeURL(element){
    var vLink = document.getElementById(element.id).value;
    if(vLink===""){
        $('#' + element.id + 'Err').removeClass('alert').delay(0).fadeOut('fast');
        return true;
    }else{
        var matches = $('#'+element.id).val().match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/);
        if (matches) {
             $('#' + element.id + 'Err').removeClass('alert').delay(0).fadeOut('fast');
             return true;
        } else {
            $('#' + element.id + 'Err').fadeIn('fast').addClass('alert').removeClass('success');
            return false;
        }
    }
}
            
$(document).ready(function(){
    var left = 500;
    $('#charcounter').text('Characters left: ' + left);
    $('#message').keyup(function () {
        left = 500 - $(this).val().length;
        if(left < 0){
            $('#charcounter').addClass("overlimit");
        }
        if(left >= 0){
            $('#charcounter span').removeClass("overlimit");
        }
        $('#charcounter').text('Characters left: ' + left);
    });
});


$.fn.upload = function(remote,data,successFn,progressFn) {
        // if we dont have post data, move it along
        if(typeof data != "object") {
                progressFn = successFn;
                successFn = data;
        }
        return this.each(function() {
                if($(this)[0].files[0]) {
                        var formData = new FormData();
                        formData.append($(this).attr("name"), $(this)[0].files[0]);
                        // if we have post data too
                        if(typeof data == "object") {
                                for(var i in data) {
                                        formData.append(i,data[i]);
                                }
                        }
                        // do the ajax request
                        $.ajax({
                                url: remote,
                                type: 'POST',
                                xhr: function() {
                                        myXhr = $.ajaxSettings.xhr();
                                        if(myXhr.upload && progressFn){
                                                myXhr.upload.addEventListener('progress',function(prog) {
                                                        var value = ~~((prog.loaded / prog.total) * 100);
                                                        // if we passed a progress function
                                                        if(progressFn && typeof progressFn == "function") {
                                                                progressFn(prog,value);
                                                        // if we passed a progress element
                                                        } else if (progressFn) {
                                                                $(progressFn).val(value);
                                                        }
                                                }, false);
                                        }
                                        return myXhr;
                                },
                                data: formData,
                                dataType: "json",
                                cache: false,
                                contentType: false,
                                processData: false,
                                complete : function(res) {
                                        var json;
                                        try {
                                                json = JSON.parse(res.responseText);
                                        } catch(e) {
                                                json = res.responseText;
                                        }
                                        if(successFn) successFn(json);
                                }
                        });
                }
        });
};